﻿/*
' Copyright (c) 2012  DotNetNuke Corporation
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
*/

using System;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Common.Controls;
using DotNetNuke.Modules.TaskManager.Components;
using System.Web.UI.WebControls;
using DotNetNuke.UI.Utilities;
using System.Web.UI.HtmlControls;

namespace DotNetNuke.Modules.TaskManager
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The View class displays the content
    /// 
    /// Typically your view control would be used to display content or functionality in your module.
    /// 
    /// View may be the only control you have in your project depending on the complexity of your module
    /// 
    /// Because the control inherits from TaskManagerModuleBase you have access to any custom properties
    /// defined there, as well as properties from DNN such as PortalId, ModuleId, TabId, UserId and many more.
    /// 
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class View : TaskManagerModuleBase, IActionable
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    rptTaskList.DataSource = TaskController.GetTasks(ModuleId);
                    rptTaskList.DataBind();
                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        public ModuleActionCollection ModuleActions
        {
            get
            {
                var actions = new ModuleActionCollection
                    {
                        {
                            GetNextActionID(), 
                            Localization.GetString("EditModule", LocalResourceFile), 
                            "", "", "",
                            EditUrl(), 
                            false, SecurityAccessLevel.Edit, true, false
                        }
                    };
                return actions;
            }
        }

        protected void rptTaskList_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                var lnkEdit = e.Item.FindControl("lnkEdit") as HtmlAnchor;
                var lnkDelete = e.Item.FindControl("lnkDelete") as LinkButton;
                var pnlAdminControls = e.Item.FindControl("pnlAdmin") as Panel;

                var curTask = (Task)e.Item.DataItem;

                if (IsEditable && lnkDelete != null && lnkEdit != null && pnlAdminControls != null)
                {
                    pnlAdminControls.Visible = true;
                    lnkEdit.HRef = EditUrl(String.Empty, String.Empty, "Edit", "tid=" + curTask.TaskId.ToString());
                    lnkEdit.InnerText = Localization.GetString("EditTask.Text", LocalResourceFile);
                    lnkDelete.CommandArgument = curTask.TaskId.ToString();
                    lnkDelete.Enabled = lnkDelete.Visible = true;

                    ClientAPI.AddButtonConfirm(lnkDelete, Localization.GetString("ConfirmDelete", LocalResourceFile));                    
                }
                else
                {
                    pnlAdminControls.Visible = false;
                }
            }
        }

        protected void rptTaskList_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
        {
            //if (e.CommandName == "Edit")
            //{
            //    var url = EditUrl(String.Empty, String.Empty, "Edit", "tid=" + e.CommandArgument);
            //    Response.Redirect(url);                
            //}
            if (e.CommandName == "Delete")
            {
                TaskController.DeleteTask(Convert.ToInt32(e.CommandArgument));
            }

            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL());

        }
    }
}